package com.ald.crossex.service;

import com.ald.crossex.dao.CeOrderDao;
import com.ald.crossex.dao.SendOrderDao;
import com.ald.crossex.dto.JqGridRequest;
import com.ald.crossex.dto.SendOrderDto;
import com.ald.crossex.entity.TCeOrder;
import com.ald.crossex.entity.VSendOrder;
import com.ald.crossex.util.JqGridFilterUtils;
import com.ald.crossex.util.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lishijiong on 2015-01-21.
 */

@Service
public class OrderService {

    @Autowired
    private HibernateTemplate hibernateTemplate;

    @Autowired
    private CeOrderDao ceOrderDao;


    @Transactional
    public void saveCeOrder(TCeOrder ceOrder) {
        hibernateTemplate.saveOrUpdate(ceOrder);
    }

    @Transactional
    public void deleteCeOrder(Integer oid) {
        hibernateTemplate.bulkUpdate("update TCeOrder set isDelete = 1 where oid = ?", new Object[]{oid});
    }

    public List<TCeOrder> queryCeOrders(Integer cityId) {
        return hibernateTemplate.find("from TCeOrder where gid= (select gid from TCeGoods where cityId = ?)  and isDelete = 0", new Object[]{cityId});
    }

    public Page queryCeOrders(Page page) {


        String hql = "from TCeOrder ";
        String count = "select * from TCeOrder";
        List<TCeOrder> list = ceOrderDao.queryPageByHQL(count, hql, new Object[]{}, page);
        page.setDatas(list);
        return page;
    }

    @Autowired
    private SendOrderDao sendOrderDao;

    /**
     * 我的未完成的订单
     *
     * @param uid
     * @return
     */
    public Page myUnfinishOrder(Integer uid) {

        Page page = new Page();
        String selectCount = "select  count(*) from VSendOrder o where 1=1 ";
        String select = "from VSendOrder o where 1=1 ";
        String condition = " and o.status <> 3 and o.uid = ?";
        List<VSendOrder> list = sendOrderDao.queryPageByHQL(selectCount + condition, select + condition, new Object[]{uid}, page);
        List<SendOrderDto> dtos = new ArrayList<SendOrderDto>();
        for (VSendOrder o : list) {
            SendOrderDto dto = new SendOrderDto();
            BeanUtils.copyProperties(o, dto);
            dtos.add(dto);

        }

        page.setDatas(dtos);

        return page;
    }

    /**
     * 我的已完成的订单
     *
     * @param uid
     * @return
     */
    public Page myFinishedOrder(Integer uid) {

        Page page = new Page();
        String selectCount = "select  count(*) from VSendOrder o where 1=1 ";
        String select = "from VSendOrder o where 1=1 ";
        String condition = "and o.status = 3 and o.uid = ?";
        List<VSendOrder> list = sendOrderDao.queryPageByHQL(selectCount + condition, select + condition, new Object[]{uid}, page);
        List<SendOrderDto> dtos = new ArrayList<SendOrderDto>();
        for (VSendOrder o : list) {
            SendOrderDto dto = new SendOrderDto();
            BeanUtils.copyProperties(o, dto);
            dtos.add(dto);

        }

        page.setDatas(dtos);

        return page;
    }
    /**
     * 系统后台查询未完成的订单
     *
     * @return
     */
    public Page unfinishOrder(JqGridRequest request) {
        Page page = new Page(request.getPage(), request.getRows());

        String conditionSql = " where 1=1  and o.status <> 3 ";

        if (StringUtils.isNotEmpty(request.getFilters())) {
            conditionSql += JqGridFilterUtils.convertJqGridFilter2String(request.getoFilters());
        }

        String orderSql = " order by " + (StringUtils.isEmpty(request.getSidx()) ? "oid" : request.getSidx()) + " " + request.getSord();

        List<VSendOrder> list = sendOrderDao.queryPageByHQL("select  count(*) from VSendOrder o " + conditionSql, "from VSendOrder o" + conditionSql + orderSql, new Object[]{}, page);
        List<SendOrderDto> dtos = new ArrayList<SendOrderDto>();
        for (VSendOrder o : list) {
            SendOrderDto dto = new SendOrderDto();
            BeanUtils.copyProperties(o, dto);
            dtos.add(dto);

        }

        page.setDatas(dtos);

        return page;
    }

    /**
     * 系统后台查询已完成的订单
     *
     * @return
     */
    public Page finishedOrder(JqGridRequest request) {
        Page page = new Page(request.getPage(), request.getRows());

        String conditionSql = " where 1=1  and o.status = 3 ";

        if (StringUtils.isNotEmpty(request.getFilters())) {
            conditionSql += JqGridFilterUtils.convertJqGridFilter2String(request.getoFilters());
        }

        String orderSql = " order by " + (StringUtils.isEmpty(request.getSidx()) ? " oid" : request.getSidx()) + " " + request.getSord();

        List<VSendOrder> list = sendOrderDao.queryPageByHQL("select  count(*) from VSendOrder o " + conditionSql, "from VSendOrder o " + conditionSql + orderSql, new Object[]{}, page);
        List<SendOrderDto> dtos = new ArrayList<SendOrderDto>();
        for (VSendOrder o : list) {
            SendOrderDto dto = new SendOrderDto();
            BeanUtils.copyProperties(o, dto);
            dtos.add(dto);

        }

        page.setDatas(dtos);

        return page;
    }
}
